package com.xiaobao.coalproperty.module.home.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xiaobao.coalproperty.MainActivity;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.home.model.entity.IndexEntity;
import com.xiaobao.coalproperty.other.adapter.GlideImageLoader;
import com.xiaobao.coalproperty.other.adapter.ProjectAdapter;
import com.xiaobao.coalproperty.other.base.BaseFragment;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.xiaobao.coalproperty.other.widget.ChartView;
import com.youth.banner.Banner;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

public class HomeFragment extends BaseFragment<MainActivity> {


    @BindView(R.id.recycler)
    XRecyclerView mXRecyclerView;

    private View headView;
    private Banner mBanner;
    private ChartView mChartView;
    private LinearLayout llCoal;

    private ProjectAdapter mProjectAdapter;
    private List<String> xData;
    private List<Float> yData;
    private List<String> imageStrings;

    @Override
    protected int getContentView() {
        return R.layout.recycler;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        imageStrings = new ArrayList<>();

        headView = LayoutInflater.from(getSuperActivity()).inflate(R.layout.fragment_home, null, false);

        mBanner = headView.findViewById(R.id.home_banner);
        mChartView = headView.findViewById(R.id.home_chart);
        llCoal = headView.findViewById(R.id.home_coal);

        llCoal.setOnClickListener(mOnClickListener);

        mProjectAdapter = new ProjectAdapter(getSuperActivity());
        mProjectAdapter.setOnItemClickListener(mOnItemClickListener);

        mXRecyclerView.setLayoutManager(new LinearLayoutManager(getSuperActivity(), LinearLayoutManager.VERTICAL, false));
        mXRecyclerView.addHeaderView(headView);
        mXRecyclerView.setLoadingListener(mLoadingListener);
        mXRecyclerView.setAdapter(mProjectAdapter);

        mBanner.setImageLoader(new GlideImageLoader());

        updateIndex();
    }

    private XRecyclerView.LoadingListener mLoadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            updateIndex();
            mXRecyclerView.refreshComplete();
        }

        @Override
        public void onLoadMore() {
            mXRecyclerView.loadMoreComplete();
        }
    };

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.home_coal:
                    startActivity(new Intent(getSuperActivity(), CoalDetailsActivity.class));
                    break;
            }
        }
    };

    private ProjectAdapter.onItemClickListener mOnItemClickListener = new ProjectAdapter.onItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getSuperActivity(),PropertyDetailsActivity.class);
            intent.setAction("id");
            intent.putExtra("id",mProjectAdapter.getAdapterData().get(position).getId());
            startActivity(intent);
        }
    };

    private void updateIndex() {
        OkHttpUtils.post().url(UrlConstant.URL_INDEX)
                .params(HttpHelperUtils.getInstance().indexParams())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    IndexEntity entity = new Gson().fromJson(response, IndexEntity.class);
                    if (entity.getCode() == 0) {
                        updateIndexSuccess(entity);
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {

                }
            }
        });
    }

    private void updateIndexSuccess(IndexEntity entity) {
        xData = new ArrayList<>();
        yData = new ArrayList<>();
        imageStrings = new ArrayList<>();
        for (int i = 0; i < entity.getData().getCarousel().size(); i++) {
            imageStrings.add(entity.getData().getCarousel().get(i).getPicture());
        }

        for (int i = 0; i < entity.getData().getPrice_info().getDate().size(); i++) {
            xData.add(entity.getData().getPrice_info().getDate().get(i));
        }

        for (int i = 0; i < entity.getData().getPrice_info().getPrice().size(); i++) {
            yData.add(Float.valueOf(entity.getData().getPrice_info().getPrice().get(i)));
        }

        mBanner.setImages(imageStrings);
        mBanner.start();
        mChartView.setData(xData, yData);
        mProjectAdapter.setAdapterData(entity.getData().getGoods());

    }
}
